package com.lzy.ninegrid;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Jinni 2017-08-31 10:29:41
 */
public class ActionSheet {
    private Context                     mContext;
    private Dialog                      mDialog;
    private TextView                    mTxtTitle;
    private LinearLayout                mLayoutContent;
    private ScrollView                  mScrollView;
    private boolean                     isTitleShow = false;
    private List<SheetItem>             mSheetItems;
    private Display                     mDisplay;

    public ActionSheet(Context context) {
        this.mContext = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mDisplay = windowManager.getDefaultDisplay();
    }

    /**从屏幕下方弹出并且可点击的弹框控件*/
    public ActionSheet builder() {

        View view = LayoutInflater.from(mContext).inflate(R.layout.view_actionsheet, null);
        view.setMinimumWidth(mDisplay.getWidth());
        mScrollView = (ScrollView) view.findViewById(R.id.sLayout_content);
        mLayoutContent = (LinearLayout) view.findViewById(R.id.lLayout_content);
        mTxtTitle = (TextView) view.findViewById(R.id.txt_title);
        TextView cancel = (TextView) view.findViewById(R.id.txt_cancel);
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });

        mDialog = new Dialog(mContext, R.style.ActionSheetDialogStyle);
        mDialog.setContentView(view);
        Window dialogWindow = mDialog.getWindow();
        if (dialogWindow != null) {
            dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.x = 0;
            lp.y = 0;
            dialogWindow.setAttributes(lp);
        }
        return this;
    }

    public ActionSheet setTitle(String title) {
        isTitleShow = true;
        mTxtTitle.setVisibility(View.VISIBLE);
        mTxtTitle.setText(title);
        return this;
    }

    public ActionSheet setCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }


    public ActionSheet setCanceledOnTouchOutside(boolean cancel) {
        mDialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public ActionSheet addSheetItem(String strItem, SheetItemColor color,
                                    OnSheetItemClickListener listener) {
        if (mSheetItems == null) {
            mSheetItems = new ArrayList<>();
        }
        mSheetItems.add(new SheetItem(strItem, color, listener));
        return this;
    }

    private void setSheetItems() {
        if (mSheetItems == null || mSheetItems.size() <= 0) {
            return;
        }
        int size = mSheetItems.size();
        if (size >= 8) {
            LayoutParams params = (LayoutParams) mScrollView
                    .getLayoutParams();
            params.height = mDisplay.getHeight() * 3 / 5;
            mScrollView.setLayoutParams(params);
        }
        for (int i = 1; i <= size; i++) {
            final int index = i;
            SheetItem sheetItem = mSheetItems.get(i - 1);
            String strItem = sheetItem.name;
            SheetItemColor color = sheetItem.color;
            final OnSheetItemClickListener listener = sheetItem.itemClickListener;
            TextView textView = new TextView(mContext);
            textView.setText(strItem);
            textView.setTextSize(16);
            textView.setGravity(Gravity.CENTER);

            if (size == 1) {
                if (isTitleShow) {
                    textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
                } else {
                    textView.setBackgroundResource(R.drawable.actionsheet_single_selector);
                }
            } else {
                if (isTitleShow) {
                    if (i >= 1 && i < size) {
                        textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
                    } else {
                        textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
                    }
                } else {
                    if (i == 1) {
                        textView.setBackgroundResource(R.drawable.actionsheet_top_selector);
                    } else if (i < size) {
                        textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
                    } else {
                        textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
                    }
                }
            }


            if (color == null) {
                textView.setTextColor(Color.parseColor(SheetItemColor.Blue
                        .getName()));
            } else {
                textView.setTextColor(Color.parseColor(color.getName()));
            }
            float scale = mContext.getResources().getDisplayMetrics().density;
            int height = (int) (45 * scale + 0.5f);
            textView.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT, height));
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(index);
                    mDialog.dismiss();
                }
            });

            mLayoutContent.addView(textView);
        }
    }

    public void show() {
        setSheetItems();
        mDialog.show();
    }

    public interface OnSheetItemClickListener {
        void onClick(int which);
    }

    public class SheetItem {
        String name;
        OnSheetItemClickListener itemClickListener;
        SheetItemColor color;

        public SheetItem(String name, SheetItemColor color,
                         OnSheetItemClickListener itemClickListener) {
            this.name = name;
            this.color = color;
            this.itemClickListener = itemClickListener;
        }
    }

    public enum SheetItemColor {
        Blue("#67a0ff"), Red("#FD4A2E");
        private String name;

        SheetItemColor(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
